package com.example.demo.serivce.impl;

import com.example.demo.core.config.SipConfig;
import com.example.demo.core.util.SipUtil;
import gov.nist.javax.sip.message.SIPRequest;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.sip.*;
import javax.sip.address.Address;
import javax.sip.address.SipURI;
import javax.sip.header.*;
import javax.sip.message.Request;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


@Service
public class ISIPCommanderImpl  {
    @Resource
    SipConfig sipConfig;

    SipProvider tcpSipProvider;
    @Autowired
    public ISIPCommanderImpl(@Lazy SipProvider tcpSipProvider) {
        this.tcpSipProvider = tcpSipProvider;
    }

    int i=0;

    public void deviceInfoQuery(RequestEvent requestEvent,String deviceId,String charset) throws InvalidArgumentException, SipException, ParseException {
        SIPRequest sipRequest = (SIPRequest)requestEvent.getRequest();
        String remoteAddress = sipRequest.getPeerPacketSourceAddress().getHostAddress();
        int remotePort = sipRequest.getPeerPacketSourcePort();
        StringBuffer catalogXml = new StringBuffer(200);
        catalogXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n");
        catalogXml.append("<Query>\r\n");
        catalogXml.append("<CmdType>DeviceInfo</CmdType>\r\n");
        catalogXml.append("<SN>" + (int) ((Math.random() * 9 + 1) * 100000) + "</SN>\r\n");
        catalogXml.append("<DeviceID>" + deviceId + "</DeviceID>\r\n");
        catalogXml.append("</Query>\r\n");
        // sipuri

       createMessageRequest(deviceId, catalogXml.toString(), SipUtil.getNewFromTag(),
                tcpSipProvider.getNewCallId(), "tcp", remoteAddress, remotePort);

    }


    public void createMessageRequest(String deviceId, String content, String fromTag,  CallIdHeader callIdHeader,String transport,String remoteAddress,int remotePort)
            throws ParseException, InvalidArgumentException, SipException {
        Request request = null;
        SipURI requestURI = SipFactory.getInstance().createAddressFactory().createSipURI(deviceId, remoteAddress+":"+remotePort);
        // via
        ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
        ViaHeader viaHeader = SipFactory.getInstance().createHeaderFactory().createViaHeader(remoteAddress, sipConfig.getPort(), transport, SipUtil.getNewViaTag());
        viaHeader.setRPort();
        viaHeaders.add(viaHeader);
        // from
        SipURI fromSipURI = SipFactory.getInstance().createAddressFactory().createSipURI(sipConfig.getId(), sipConfig.getDomain());
        Address fromAddress = SipFactory.getInstance().createAddressFactory().createAddress(fromSipURI);
        FromHeader fromHeader = SipFactory.getInstance().createHeaderFactory().createFromHeader(fromAddress, fromTag);
        // to
        SipURI toSipURI = SipFactory.getInstance().createAddressFactory().createSipURI(deviceId,remoteAddress+":"+remotePort);
        Address toAddress = SipFactory.getInstance().createAddressFactory().createAddress(toSipURI);
        ToHeader toHeader = SipFactory.getInstance().createHeaderFactory().createToHeader(toAddress, SipUtil.getNewFromTag());

        // Forwards
        MaxForwardsHeader maxForwards = SipFactory.getInstance().createHeaderFactory().createMaxForwardsHeader(70);

        // ceq
        CSeqHeader cSeqHeader = SipFactory.getInstance().createHeaderFactory().createCSeqHeader(++i, Request.MESSAGE);

        request = SipFactory.getInstance().createMessageFactory().createRequest(requestURI, Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader,
                toHeader, viaHeaders, maxForwards);
        List<String> agentParam = new ArrayList<>();
        agentParam.add("sky");
        request.addHeader(SipFactory.getInstance().createHeaderFactory().createUserAgentHeader(agentParam));

        ContentTypeHeader contentTypeHeader = SipFactory.getInstance().createHeaderFactory().createContentTypeHeader("Application", "MANSCDP+xml");
        request.setContent(content, contentTypeHeader);
        tcpSipProvider.sendRequest(request);

    }



}
